package com.ruoyi.web.cp.service;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.text.CharSequenceUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.nio.charset.Charset;
import java.util.*;
import java.util.stream.Collectors;

import static com.ruoyi.web.cp.service.NumberChecker.SKIP_NUMBER2;

@Slf4j
@Service
public class SscService {
    private static final String ISSUE = "issue";
    private static final String MM = "mm";
    @Autowired
    private HttpService httpService;
    @Autowired
    private WinxinService winxinService;

//    @Async
    public void checkSameCodes(String ticketId, String name, Map<String, Object> cache) {
        checkSameCodes(ticketId, name, cache, 1000);
    }

//    @Async
    public void checkSameCodes(String ticketId, String name, Map<String, Object> cache, Integer interval) {
        String mm = DateUtil.format(new Date(), MM);
        String cacheMM = (String) cache.getOrDefault(name + MM, "0");
        if (mm.equals(cacheMM)) {
            return;
        }
        if (((Integer.parseInt(cacheMM) * 1000 + interval) % 60000) > Integer.parseInt(mm) * 1000) {
            return;
        }

        JSONObject data = httpService.sendLeyuCpList(ticketId);
        List<String> lists = new ArrayList<>();
        List<String[]> lists2 = new ArrayList<>();
        if ("0".equals(data.getString("code"))) {
            JSONArray items = data.getJSONArray("data");
            String issue = items.getJSONObject(0).getString(ISSUE);
            if (issue.equals(cache.get(name + ISSUE))) {
                return;
            }
            log.info("{},{},issue:{},code:{}", ticketId, name, issue, items.getJSONObject(0).getString("code"));
            cache.put(name + ISSUE, issue);
            cache.put(name + MM, mm);
            for (int i = 0; i < SKIP_NUMBER2; i++) {
                if (i < 5) {
                    lists.add(items.getJSONObject(i).getString("code"));
                }
                lists2.add(items.getJSONObject(i).getString("code").split(","));
            }
            checkSameCodes(lists, name, issue);
            checkSamePosition(lists, name, issue);
            if (NumberChecker.isSkipNumbers(lists2)) {
                String msg = CharSequenceUtil.format("{},issue:{},前4跳号", name, issue);
                log.info(msg);
                winxinService.sendTextMessage("1000009", msg);
            }
            if (NumberChecker.isSkipNumbers2(lists2)) {
                String msg = CharSequenceUtil.format("{},issue:{},连续跳号4", name, issue);
                log.info(msg);
                winxinService.sendTextMessage("1000015", msg);
            }
        }
    }

    public JSONObject readJson() {
        String str = FileUtil.readString("D:\\乱七八糟\\57.txt", Charset.forName("utf8"));
        return JSON.parseObject(str);
    }

    public void checkSameCodes(List<String> array, String name, String issue) {
        if (array == null || array.isEmpty()) {
            return;
        }
        List<String[]> datas = array.stream().map(item -> item.split(",")).collect(Collectors.toList());
        Map<String, Set<String>> map = new LinkedHashMap<>();
        for (int j = 0; j < 5; j++) {
            for (int i = 0; i < datas.size(); i++) {
                String[] data = datas.get(i);
                Set<String> set = map.getOrDefault(j + "", new HashSet<>());
                set.add(data[j]);
                map.put(j + "", set);
            }
        }
        Iterator<String> iterator = map.keySet().iterator();
        while (iterator.hasNext()) {
            String key = iterator.next();
            Set<String> set = map.get(key);
            if (set.size() == 1) {
                String msg = CharSequenceUtil.format("{},issue:{},{},{},连续同号:{}", name, issue, key, set, array.size());
                log.info(msg);
                winxinService.sendTextMessage("1000015", msg);
            }
        }
    }

    public void checkSamePosition(List<String> array, String name, String issue) {
        if (array == null || array.isEmpty()) {
            return;
        }
        List<String[]> datas = array.stream().map(item -> item.split(",")).collect(Collectors.toList());
        Integer[][] indexs = {{0, 1}, {1, 2}, {2, 3}, {3, 4}, {0, 2}, {0, 3}, {0, 4}, {1, 3}, {1, 4}, {2, 4}};
        Map<String, Integer> map = new LinkedHashMap<>();
        for (int i = 0; i < datas.size(); i++) {
            for (int j = 0; j < indexs.length; j++) {
                String key = indexs[j][0] + "" + indexs[j][1];
                if (datas.get(i)[indexs[j][0]].equals(datas.get(i)[indexs[j][1]])) {
                    int num = map.getOrDefault(key, 0);
                    map.put(key, ++num);
                }
            }
        }
        Iterator<String> iterator = map.keySet().iterator();
        while (iterator.hasNext()) {
            String key = iterator.next();
            int num = map.get(key);
            if (num == array.size()) {
                String msg = CharSequenceUtil.format("{},issue:{},{},连续和:{}", name, issue, key, array.size());
                log.info(msg);
                winxinService.sendTextMessage("1000015", msg);
            }
        }
    }


    public static void main(String[] args) {
        String mm = "17";
        Integer interval = 5000;
        String cacheMM = "12";

        System.out.println((Integer.parseInt(cacheMM) * 1000 + interval) <= Integer.parseInt(mm) * 1000);
    }
}
